package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.databinding.ActivityHomeBinding
import com.hinnotek.amoeba.ui.fragment.FragmentHomeManagerMsg
import com.hinnotek.amoeba.ui.fragment.FragmentHomeManagerWork
import com.hinnotek.amoeba.ui.fragment.FragmentHomeMsg
import com.hinnotek.amoeba.ui.fragment.FragmentHomeWork
import com.hinnotek.amoeba.utilities.StatusBarUtil
import com.hinnotek.amoeba.viewmodels.ManageViewModel
import com.hinnotek.amoeba.widget.ITitleBarListener
import com.hinnotek.amoeba.widget.MyTitleBar
import com.lee.common.base.BaseActivity
import com.lee.common.utils.*
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@Route(path = ACTIVITY_MAIN_OLD)
@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityHomeBinding>(), ViewPager.OnPageChangeListener,
    ITitleBarListener {

    //记录用户首次点击返回键的时间
    private var firstTime: Long = 0
    private var fragments: MutableList<Fragment> = mutableListOf()
    private var isLogin = false
    private var roleId = 4 //默认当前用户角色是普通员工
    private lateinit var mTitleBar: MyTitleBar
    private var fragmentHomeWork: FragmentHomeWork? = null

    private val manageViewModel by viewModels<ManageViewModel>()


    override fun initVew() {
        //2020-3-26,张洋添加
        setTitleBarGone()
        mTitleBar = MyTitleBar(this)
        mTitleBar.setTitleBarListener(this)
        mTitleBar.setLeftVisible(false)
        mTitleBar.setRightVisible(false)
        bindingView.llMainActivityHome.addView(mTitleBar, 0)
        StatusBarUtil.setStatusBarColor(this, R.color.colorbtns)
        //bindingView.flHome.setSlide(false)
        fragments = ArrayList()
        if (SharedPreUtils.getInteger("roleId", 0) == 1) {
            fragments.add(FragmentHomeManagerMsg())
            fragments.add(FragmentHomeManagerWork())
        } else {
            if (SharedPreUtils.getInteger(isAmoebaOrCompany, 0) == 1) {
                if (SharedPreUtils.getBoolean(isNoticeApprove, false)) {
                    fragments.add(FragmentHomeMsg())
                } else {

                    bindingView.homeMain.visibility = View.GONE
                }
            } else {
                if (SharedPreUtils.getBoolean(isCompanyFinance, false)) {
                    fragments.add(FragmentHomeManagerMsg())
                } else {
                    if (SharedPreUtils.getBoolean(isNoticeApprove, false)) {
                        fragments.add(FragmentHomeMsg())
                    } else {
                        bindingView.homeMain.visibility = View.GONE
                    }
                }
            }
            fragments.add(FragmentHomeWork().also { fragmentHomeWork = it })
        }
        bindingView.flHome.adapter = object : FragmentPagerAdapter(this.supportFragmentManager) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(position: Int): Fragment {
                return fragments.get(position)
            }


        }
        bindingView.flHome.setOnPageChangeListener(this)

        bindingView.homeBottomBar.check(R.id.home_celue)
        bindingView.flHome.currentItem = if (fragments.size == 2) 1 else 0
        mTitleBar.setTitleValue("工作台")
        bindingView.homeBottomBar.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.home_main -> {
                    bindingView.flHome.currentItem = 0
                    mTitleBar.setTitleValue("消息")
                }
                R.id.home_celue -> {
                    bindingView.flHome.currentItem = if (fragments.size == 2) 1 else 0
                    mTitleBar.setTitleValue("工作台")
                    if (fragmentHomeWork != null) {
                        fragmentHomeWork?.setFresh()
                    }
                }
            }
        }
    }

    override fun setData() {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        super.onCreate(savedInstanceState)
        isLogin = SharedPreUtils.getBoolean("isLogin", false)
        roleId = SharedPreUtils.getInteger("roleId", 4) //默认当前用户角色是普通员工
        setContentView(R.layout.activity_home)
    }



    override fun finish() {
        val view: ViewGroup = getWindow().getDecorView() as ViewGroup
        view.removeAllViews()
        super.finish()
    }


    override fun onPageScrollStateChanged(state: Int) {}

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

    override fun onPageSelected(position: Int) {
        when (if (fragments.size == 2) position else position + 1) {
            0 -> bindingView.homeBottomBar.check(R.id.home_main)
            1 -> bindingView.homeBottomBar.check(R.id.home_celue)
            else -> {
            }
        }
    }

    override fun onLeftLayoutListener() {}
    override fun onRightLayoutListener() {}


}